<div class="sm-pop">
    <blockquote class="site-text layui-elem-quote" id="msgType_select">
        消息类型 ：
        <button class="layui-btn btn-primary layui-btn-radius">文本消息</button>
        <button class="layui-btn layui-btn-primary layui-btn-radius">图文消息</button>
        <button class="layui-btn layui-btn-primary layui-btn-radius">图片消息</button>
        <button class="layui-btn layui-btn-primary layui-btn-radius">语音消息</button>
        <button class="layui-btn layui-btn-primary layui-btn-radius">视频消息</button>
    </blockquote>
    <div id="send_msg_content">
        <table id="select_table" style="" class="layui-hide" lay-filter="select_table"></table>
        
    </div>
</div>
<script type="text/javascript">
layui.use(['layer', 'table'], function () {
        var layer = layui.layer;
        var $ = layui.$;
        var table = layui.table;
        $("#msgType_select").on("click", ".layui-btn", function () {
            var $this = $(this);
            var type = "text";
            switch ($this.index()) {
                case 1:
                    type = "news";
                    break;
                case 2:
                    type = "image";
                    break;
                case 3:
                    type = "voice";
                    break;
                case 4:
                    type = "video";
                    break;
                default:
                    type = "text"
                    break;
            }
            $this.addClass("btn-primary").removeClass("layui-btn-primary")
                .siblings(".btn-primary").addClass("layui-btn-primary").removeClass("btn-primary");
            pageTable(type);
        });
        pageTable("text");
        var tableObj=null;
        function pageTable(type){
        	
         	if(type){
         		var fieldType="msgtype";
         		var fieldId="id";
         		var url='';
         		if(type == 'text'){
         			url="/msgtext/list"
         		}else if(type == 'news'){
         			url="/msgnews/list";
         		}else{
         			fieldType="mediaType";
         			fieldId="baseId";
         			url="/mediaFile/list?mediaType="+type;
         		}
         		var options={
                        id: 'select_table',
                        elem: '#select_table',
                        url: url,
                        align: "center",
                        cols: [[ //表头
                            {type: 'numbers'},
                            {
                                field: fieldType, title: '类型', width: 150, align: 'center', templet: function (d) {
                                var rtype = "文本消息";
                                switch (type) {
                                    case "news":
                                        rtype = "图文消息";
                                        break;
                                    case "image":
                                        rtype = "图片消息";
                                        break;
                                    case "voice":
                                        rtype = "语音消息";
                                        break;
                                    case "video":
                                        rtype = "视频消息";
                                        break;
                                    default:
                                        rtype = "文本消息"
                                        break;
                                }
                                return rtype;
                            }
                            },
                            {field: 'title', title: '标题', width: 150, align: 'center',templet: function (d) {
                                    var rtype = d.title;
                                    switch (type) {
                                        case "image":
                                            rtype = "<img src="+d.uploadUrl+">";
                                            break;
                                    }
                                    return rtype;
                                }},
                            {field: fieldId, title: 'id', width: 80, align: 'center',},
                            {
                                field: 'lock', title: '操作', width: 150, align: 'center', templet: function (d) {
                                return '<button class="layui-btn layui-btn-sm " class="font-primary" lay-event="choose"><i class="iconfont icon-add"></i>选择</button>';

                            }, unresize: true, align: 'center'
                            }
                        ]],
                        page: true
                    }
         		if(tableObj!=null){
	         		tableObj.reload(options);
         		}else{
         			tableObj = table.render(options);
         		}
         	}
         }
        //选择资源表格内部操作按钮监听
        layui.table.on('tool(select_table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
//             if(!data.id){
//             	data.id=data.baseId;
//             }
            if(!data.msgtype){
            	data.msgtype=data.mediaType;
            }
            console.log(JSON.stringify(data));
			//这里需要判断显示的消息类型
            if (layEvent === 'choose') {
                refreshkey(data);
            }
        });
        var refreshkey = function (data) {
        	//如果需要可以把title方法dom 属性中，而不需要去查msgs
            $("input[name=key]")
                .val(data.baseId)
                .attr("data-msgtype", data.msgtype)
                .attr("data-res-title",data.title)
                .trigger("change");
            layer.closeAll();
        };
});
 </script>